package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import dao.AdminInfoDAO;
import dao.AdminInfoDaoJdbcImpl;
import dao.AnnounceDAO;
import dao.AnnounceDaoJdbcImpl;
import dao.HomeworkDAO;
import dao.HomeworkDaoJdbcImpl;
import dao.MediaDAO;
import dao.MediaDaoJdbcImpl;
import dao.UserInfoDAO;
import dao.UserInfoDaoJdbcImpl;
import modle.adminInfo;
import modle.announce;
import modle.homework;
import modle.media;
import modle.userInfo;
import modle.users;
import util.InterCheck;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String type = request.getParameter("options");
		System.out.println("username="+username + "  password:" + password + "  options=" + type);
		users user = InterCheck.interCheck(username, password);
		if( user != null ) {
			System.out.println(type.equals("options1"));
			if( type.equals("1")  ) {	//学生
				System.out.println("学生账号密码正确！");
				UserInfoDAO uid = new UserInfoDaoJdbcImpl();
				userInfo userInfo = uid.readUserInfo(username);
				if( userInfo != null ) {
					System.out.println("获取学生 信息成功");
					if( userInfo.getUStatus() == 1 ) {	//账号密码正确
						request.setAttribute("userid", username);
						request.getSession().setAttribute("user", user);
						response.sendRedirect("user_page/index.html");
					}else {
						response.setContentType("text/html;charset=gb2312");  
				        PrintWriter out = response.getWriter();  
				        out.print("<script>alert('您的账号未激活，请联系管理员激活账后后重试！');window.location='user_page/Login.jsp';</script>");
				        out.flush();
					}
				}else {
					response.setContentType("text/html;charset=gb2312");  
			        PrintWriter out = response.getWriter();  
			        out.print("<script>alert('您的账号类型不匹配，请重新选择后重试！');window.location='user_page/Login.jsp';</script>");
			        out.flush();
				}
			}else if( type.equals("2") ) {	//管理员
				System.out.println("学生账号密码正确！");
				AdminInfoDAO aid = new AdminInfoDaoJdbcImpl();
				adminInfo adminInfo = aid.readAdminInfo(username);
				if( adminInfo != null ) {
					System.out.println("获取管理员信息成功");
					request.setAttribute("userid", username);
					request.getSession().setAttribute("user", user);
					response.sendRedirect("public/page/index.html");
				}else {
					response.setContentType("text/html;charset=gb2312");  
			        PrintWriter out = response.getWriter();  
			        out.print("<script>alert('您的账号类型不匹配，请重新选择后重试！');window.location='user_page/Login.jsp';</script>");
			        out.flush();
				}
				
			}
		}else {
			response.setContentType("text/html;charset=gb2312");  
	        PrintWriter out = response.getWriter();  
	        out.print("<script>alert('账号或密码错误！');window.location='user_page/Login.jsp';</script>");
	        out.flush();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
